#include "searchstu.h"
#include "ui_searchstu.h"

#include <QList>
#include <student.h>
#include <QtDebug>
#include <QMessageBox>

#include <QTableWidget>
#include <QStringList>
#include <QStandardItem>

SearchStu::SearchStu(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SearchStu)
{
    ui->setupUi(this);

    //显示所有的数据
    QList<Student> stu_list;
    int ret = this->getStuFromFile(stu_list);
    if(ret == -1)
    {
       QMessageBox::critical(this,"错误","文件打开错误，请重试。","确定");
    }
    if(ret == 1)
    {
        this->display(stu_list);
    }


}
void SearchStu::display(QList<Student> &stu_list)
{
    //设置表头
    //标准的MVC模式
    this->model = new  QStandardItemModel;
    this->model->setHorizontalHeaderItem(0,new QStandardItem("姓名"));
    this->model->setHorizontalHeaderItem(1,new QStandardItem("学号"));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("性别"));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("年龄"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("院系"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("兴趣"));

    this->ui->tableView->setModel(this->model);

    //获取所有的student

    //所有的学生进行显示
    for(int i = 0;i<stu_list.length();i++)
    {
        Student stu = stu_list.at(i);
        QList<QString> stu_str_list = stu.toString().split(" ");
        //每一个Student
        for(int j = 0;j<5;j++)
        {
            this->model->setItem(i,j,new QStandardItem(stu_str_list.at(j)));
        }
        QString ins = "";
        for(int j = 5;j<stu_str_list.length();j++)
        {

            ins.append(stu_str_list.at(j)+",");
        }
        //删除ins的最后的逗号
        ins.remove(ins.length()-1,1);
        this->model->setItem(i,5,new QStandardItem(ins));
    }

}

SearchStu::~SearchStu()
{
    delete ui;
}

void SearchStu::on_btn_search_clicked()
{
    //从文件中获取所有的Student信息
    QList<Student> stu_list;
    int ret = this->getStuFromFile(stu_list);
    if(ret == -1)
    {
       QMessageBox::critical(this,"错误","文件打开错误，请重试。","确定");
    }
    if(ret == 1)
    {
        for(int i = 0;i<stu_list.length();i++)
        {
            Student stu = stu_list.at(i);
            qDebug()<<stu.toString()<<endl;
        }
        //如果选择按姓名查找
        if(this->ui->comboBox->currentIndex()==0)
        {
            QString search_str = this->ui->lineEdit->text();
            QList<Student> result_stu;
            for(int i = 0;i<stu_list.length();i++)
            {
                Student s = stu_list.at(i);
                if(s.getName()==search_str)
                {
                    result_stu.append(s);
                }
            }
            this->model->clear();
            display(result_stu);
        }

        //如果选择按学号查找
        if(this->ui->comboBox->currentIndex()==1)
        {
            QString search_str = this->ui->lineEdit->text();
            QList<Student> result_stu;
            for(int i = 0;i<stu_list.length();i++)
            {
                Student s = stu_list.at(i);
                if(s.getSno()==search_str)
                {
                    result_stu.append(s);
                }
            }
            this->model->clear();
            display(result_stu);
        }
    }


}

int  SearchStu::getStuFromFile(QList<Student> &stu_list)
{

    QFile file("stu.txt");
    if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        return -1;
    }
    QTextStream in(&file);
    while(!in.atEnd()){
        QString line = in.readLine();
       // qDebug()<<"line="<<line<<endl;
        Student stu = Student::getStuFromString(line.trimmed());
        stu_list.append(stu);
    }
    file.close();
    return 1;
}

